﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using Happy.Web.Mvc;
using Happy.Cms.Application.Articles;
using Ciep.WebSite.Models;

namespace Ciep.WebSite.Controllers
{
    public abstract class ArticleController : CiepController
    {
        //
        // GET: /News/

        public ActionResult Index(int? page)
        {
            var articles = this.Service<ArticleQueryService>()
                .FindByCategoryName(this.CategoryName, page ?? 1, 15);

            this.SetCommonDataToViewBag();

            return View(new PagedArticles
            {
                Title = this.CategoryName,
                DetailHref = String.Format("/{0}/Detail/", this.ControllerName),
                PageHref = String.Format("/{0}/Index", this.ControllerName),
                Articles = articles
            });
        }

        public ActionResult Top()
        {
            var articles = this.Service<ArticleQueryService>()
                .FindByCategoryName(this.CategoryName, 5);

            return View(new TopArticles
            {
                Title = this.CategoryName,
                DetailHref = String.Format("/{0}/Detail/", this.ControllerName),
                MoreHref = String.Format("/{0}/Index", this.ControllerName),
                Articles = articles
            });
        }

        public ActionResult Detail(Guid id)
        {
            var article = this.Service<ArticleQueryService>()
                .SingleOrDefault(id);

            this.SetCommonDataToViewBag();

            return View(article);
        }

        protected abstract string CategoryName { get; }

        protected override string Title
        {
            get { return this.CategoryName; }
        }
    }
}
